<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

// no direct access
defined('_JEXEC') or die('Restricted access');
//Register the session storage class with the loader

/**
 * Joomla! Cache base object
 *
 * @abstract
 * @package		Joomla.Framework
 * @subpackage	Cache
 * @since		1.5
 */
class JA_Cache{
	var $_cache = null;
	var $_cache_id = null;
        
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $cache_base
	 * @param unknown_type $cache_time
	 * @param unknown_type $caching
	 * @return JA_Cache
	 */
	function JA_Cache($cache_base='', $cache_time=5, $caching=true)
	{		
		$options = array(
			'cachebase' 	=> $cache_base,
			'defaultgroup' 	=> 'defaultgroup',
			'lifetime' 		=> (int)$cache_time*60,
			'browsercache'	=> false,
			'caching'		=> $caching,
			'language'		=> 'en-GB'
		);
		
		jimport('joomla.filesystem.folder');
		if($cache_base!='' && !JFolder::exists($cache_base)){
	       JFolder::create($cache_base, 0777);
	    }
	    
		jimport('joomla.cache.cache');
		$this->_cache =	new JCache($options);
	}
	
	/**
	 * Generate a view cache id
	 *
	 * @access	private
	 * @param	object	$view	The view object to cache output for
	 * @param	string	$method	The method name to cache for the view object
	 * @return	string	MD5 Hash : view cache id
	 * @since	1.5
	 */
	function _makeId($view='', $layout='')
	{		
		$list = '';
		$this->_cache_id = '';
		
		$view 	= $view?$view:JRequest::getVar('view', '');
		$layout = $layout?$layout:JRequest::getVar('layout', 'jalist');
		$url = JFactory::getURI();
		$host = $url->_host;
		$browser = '';//get_user_browser();
		$user = JFactory::getUser();
		switch ($view){
			case VIEW_JOBS:{
				switch ($layout){
								
					case 'jalist':
						$list .= $browser.$url->_uri;
						if(getCurrentUserType() == JBJOB_SEEKER_USER) $list .= $user->id;
						
						break;
					case 'myjobs':
						$list .= $host.$browser.$view .'&'. $layout;
						$uid = JRequest::getInt('uid', 0);
						
						if($user->id>0 && (!$uid || $uid==$user->id)){
							$list .= '&'.$user->id.'&myself';
						}
						elseif($uid>0) $list .= '&'.$uid;
						break;
					case 'jajobseekerlist':					
						$list .= $host.$browser.$view .'&'. $layout;
						if($user->id>0)	$list .= '&'.$user->id;					
						break;
				}
				if(trim(JRequest::getVar('search', '')))	 $list .= trim(JRequest::getVar('search', ''));				
			}break;
			
			case VIEW_RESUMES:{
				switch ($layout){
								
					case 'jalist':
						$list .= $browser.$url->_uri;
						if(getCurrentUserType() == JBEMPLOYER_USER) $list .= $user->id;
						break;
					case 'jaemployerlist':
						$list .= $host.$browser.$view .'&'. $layout;
						if(getCurrentUserType() == JBEMPLOYER_USER) $list .= $user->id;
						break;				
				}	
				if(trim(JRequest::getVar('resume_search', '')))	 $list .= trim(JRequest::getVar('resume_search', ''));
			}break;
			
			case VIEW_APPLICATION:{
				switch ($layout){
								
					case 'jalist':
						$list .= $browser.$url->_uri;
						if(getCurrentUserType() == JBEMPLOYER_USER) $list .= $user->id;
						break;
					case 'jaemployerlist':
						$list .= $host.$browser.$view .'&'. $layout;
						if(getCurrentUserType() == JBEMPLOYER_USER) $list .= $user->id;
						break;				
				}		
			}break;
						
			case VIEW_POSTS:{
				switch ($layout){
								
					case 'buycredit':
					case 'selectplan':
						$list .= $browser.$url->_uri;
						if(getCurrentUserType() == JBEMPLOYER_USER) $list .= $user->id;
						break;					
				}		
			}break;
			
			case VIEW_EMPLOYERS:{
				if(getCurrentUserType() == JBEMPLOYER_USER && $layout){
					$list .= $browser.$url->_uri.$user->id;
				}
				if(trim(JRequest::getVar('user_search', '')))	 $list .= trim(JRequest::getVar('user_search', ''));		
			}break;
		}
		if(JRequest::getInt('limitstart', '')){
			$list .= '_'.JRequest::getInt('limitstart', '');
		}
		if(JRequest::getInt('limit', '')){
			$list .= '_'.JRequest::getInt('limit', '');
		}		
		if(JRequest::getVar('filter_order', '')){
			$list .= JRequest::getVar('filter_order', '');
		}
		if(JRequest::getVar('filter_order_Dir', '')){
			$list .= JRequest::getVar('filter_order_Dir', '');
		}
		if($list) $this->_cache_id = md5($list);
		return $this->_cache_id;
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $cache_id
	 * @param unknown_type $view
	 * @return unknown
	 */
	function check_cache($cache_id='', $view=''){
	    if(!$this->_cache->_options['caching']) return false;
	    if($cache_id) $this->_cache_id = $cache_id;
		else $this->_cache_id = $this->_makeId();
		$view = $view?$view:JRequest::getVar('view', '');
		if($this->_cache_id!='' && $data = $this->_cache->get($this->_cache_id, $view)) {
			echo $data;
			return true;	
		}
		return false;
	
	}
	
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $view
	 * @param unknown_type $task
	 * @param unknown_type $array_clean_config
	 */
	function clean_cache($view, $task, $array_clean_config){
		if(isset($array_clean_config[$view]) && isset($array_clean_config[$view][$task]) && is_array($array_clean_config[$view][$task])){
			$cache_group_array = $array_clean_config[$view][$task];
			$count = count($cache_group_array);
			if ($count>0) {
				for ($i=0;$i<$count;$i++) {
					$this->_cache->clean($cache_group_array[$i]);
				}
			}
		}
	}			
}